///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_STATISTICS_H_
#define _BUOLA_MAT_STATISTICS_H_

#include <buola/mat/cmat.h>
#include <buola/mat/initializers.h>
#include <buola/mat/cout.h>

namespace buola { namespace mat {

CVec_d histogram(const CVec_d &pData,const CVec_d &pEdges)
{
    mat::CVec_d lH=mat::zeros(pEdges.size()-1);
    
    for(int i=0;i<pData.size();i++)
    {
        if(pData(i)<pEdges[0]) continue;

        for(int j=1;j<pEdges.size();j++)
        {
            if(pData(i)<pEdges(j))
            {
                lH(j-1)++;
                break;
            }
        }
    }
    
    return std::move(lH);
}

CVec_d histogram(const CVec_d &pData,size_t pN,double pMin=NAN,double pMax=NAN,const COut<CVec_d> &pCenters={})
{
    if(is_nan(pMin))
        pMin=min(pData);
    if(is_nan(pMax))
        pMax=max(pData);

    double lBinWidth=(pMax-pMin)/pN;
    
    mat::CVec_d lH=mat::zeros(pN);
    
    for(int i=0;i<pData.size();i++)
    {
        int j=(int)((pData(i)-pMin)/lBinWidth);
        if(j<0)
            j=0;
        else if(j>=pN)
            j=pN-1;
        lH(j)++;
    }

    if(pCenters)
    {
        pCenters=linspace(pN,pMin+lBinWidth/2,pMax-lBinWidth/2);
    }
    
    return std::move(lH);
}

CVec_d histogram(const CVec_d &pData,size_t pN,const COut<CVec_d> &pCenters={})
{
    return histogram(pData,pN,NAN,NAN,pCenters);
}

/*namespace mat*/ } /*namespace buola*/ }

#endif
